﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewStart
{
    public partial class Tile : UserControl
    {
        public static int defaultwidth = 123, largewidth = 252;
        public string path;
        public bool mouseover = false;
        Point lastMousePos;

        public Tile()
        {
            InitializeComponent();
        }

        public Tile(bool largetile, string text, string path)
        {
            InitializeComponent();

            if (largetile == true)
                this.Width = largewidth;
            else
                this.Width = defaultwidth;

            label1.Text = text;
            pictureBox1.Image = ShortcutsHelper.GetIcon(path);
            this.path = path;
            this.BackColor = SettingsHelper.settings.foreColor;
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            Graphics g = pevent.Graphics;
            Rectangle rBackground = new Rectangle(0, 0, this.Width, this.Height);
            System.Drawing.Drawing2D.LinearGradientBrush bBackground
               = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground, Color.FromArgb(0, 72, 90), Color.FromArgb(0, 89, 111), 0f);
            g.FillRectangle(bBackground, rBackground);
            bBackground.Dispose();
            ControlPaint.DrawBorder(g, rBackground, Color.FromArgb(29, 93, 110), ButtonBorderStyle.Solid);
        }

        public bool LargeTile
        {
            get
            {
                if (this.Width == defaultwidth)
                    return false;
                else
                    return true;
            }
            set
            {
                if (value == true)
                    this.Width = largewidth;
                else
                    this.Width = defaultwidth;
            }
        }

        public bool ShowText
        {
            get { return label1.Visible; }
            set { label1.Visible = value; }
        }

        public override string Text
        {
            get { return label1.Text; }
            set { label1.Text = value; }
        }

        public Image Image
        {
            get { return pictureBox1.Image; }
            set { pictureBox1.Image = value; pictureBox1.Invalidate(); }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            lastMousePos = Cursor.Position;

            if (e.Button == MouseButtons.Right)
                contextMenuStrip1.Show(this, Point.Empty);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            //if (lastMousePos.X - e.X < 20 && lastMousePos.Y - e.Y < 20)
            {
                if (e.Button == MouseButtons.Left)
                {
                    System.Diagnostics.Process.Start(path);
                    ((MenuForm)this.ParentForm).Close();
                }
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            mouseover = true;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            mouseover = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!(lastMousePos.X - e.X < 20 && lastMousePos.Y - e.Y < 20) && e.Button == MouseButtons.Left)
            {
                CycleForm.m.flowLayoutPanel1.DoDragDrop(this, DragDropEffects.Move);
            }
        }

        private void removeTileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TilesHelper.RemoveTile(this);
        }

        private void largeTileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LargeTile = !LargeTile;
            TilesHelper.ReloadLayout();
        }
    }
}
